
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;

using Swaf;
using Swaf.BizObj;

namespace Swaf.BizObj.fieldValidator
{
	/// <summary> The concrete FieldValidator derived class that validates the length of a string.
	/// </summary>
	[Serializable]
	public class LengthValidator:FieldValidator
	{
		public static void register(IApplication app)
		{
			Swaf.BizObj.BizObjManager bizObjMgr = (Swaf.BizObj.BizObjManager)app.bizObjMgr;
			// Prototype Pattern: adds a prototype object to FieldValidator's (static) "prototypes_" map.
			reg(bizObjMgr, "length{ws}()({integer}){ws}to{ws}({integer})", "length 5 to 7", 5, 7);
			reg(bizObjMgr, "length{optional-ws}(==){optional-ws}({integer})", "length == 5", - System.Int32.MaxValue, 5);
			reg(bizObjMgr, "length{optional-ws}(<=){optional-ws}({integer})", "length <= 5", - System.Int32.MaxValue, 5);
			reg(bizObjMgr, "length{optional-ws}(>=){optional-ws}({integer})", "length >= 5", 5, System.Int32.MaxValue);
			reg(bizObjMgr, "not-empty", "not-empty", 1, System.Int32.MaxValue);
		}
		
		
		/// <summary>Registers the specified pattern as a validator.
		/// </summary>
		protected internal static void  reg(BizObjManager bizObjMgr, string pattern, string example, int min, int max)
		{
			bizObjMgr.registerPrototype(pattern, new LengthValidator(example, min, max));
		}
		
		public override string validateNoThrow(IBizObj bizObj, string name, string newValue)
		{
			int x = newValue.Length;
			if (x < min_)
				return "Must not be shorter than " + min_ + " chars";
			if (x > max_)
				return "Must not be longer than " + max_ + " chars";
			return null;
		}
		
		protected internal override FieldValidator create(string context, string a, string b, string c)
		{
			return new LengthValidator(context, a, b, c);
		}
		
		/// <summary>Constructs a LengthValidator using the specified name and params.
		/// </summary>
		protected internal LengthValidator(string context, string a, string b, string c):base(context)
		{
			if (Cmn.IsEmpty(a) && Cmn.IsEmpty(b) && Cmn.IsEmpty(c))
			{
				min_ = 1;
				max_ = System.Int32.MaxValue;
			}
			else if (a.Equals(""))
			{
				min_ = toInt(context, b);
				max_ = toInt(context, c);
			}
			else if (a.Equals("=="))
			{
				min_ = toInt(context, b);
				max_ = min_;
			}
			else if (a.Equals(">="))
			{
				min_ = toInt(context, b);
				max_ = System.Int32.MaxValue;
			}
			else if (a.Equals("<="))
			{
				min_ = 0;
				max_ = toInt(context, b);
			}
			else
			{
				parseError("unknown name: " + context);
			}
			if (min_ > max_)
				parseError("min and max lengths are backwards: " + context);
			if (min_ < 0 || max_ < 0)
				parseError("unexpected negative number: " + context);
		}
		
		/// <summary>Constructs a LengthValidator using the specified min and max.
		/// </summary>
		protected internal LengthValidator(string context, int min, int max):base(context)
		{
			min_ = min;
			max_ = max;
		}
		
		/// <summary>This is the minimum length of a valid string.
		/// </summary>
		protected internal int min_;
		
		/// <summary>This is the maximum length of a valid string.
		/// </summary>
		protected internal int max_;
		
		public override string details(string prefix)
		{
			return prefix + "LengthValidator(\"" + this + "\") { min=" + min_ + ", max=" + max_ + " }";
		}
	}
}